package com.zqwh.yj.sdk.utils;

import android.os.Build;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class LogUtils {
    private static final String LOG_PATH = SDCardUtils.getInnerSDCardPath()+"/log/";
    private static String  tag            = "zqwh";

    public static void debug(String log) {
        String logAll = "[" + Thread.currentThread().getStackTrace()[3].getFileName() + "," +
                Thread.currentThread().getStackTrace()[3].getMethodName() + "," +
                Thread.currentThread().getStackTrace()[3].getLineNumber() + "]" + "  " + log;
        if (log != null) {
            Log.d(tag, logAll);

        }
    }

    public static void logout(String log) {
        String logAll = "[" + Thread.currentThread().getStackTrace()[3].getFileName() + "," +
                Thread.currentThread().getStackTrace()[3].getMethodName() + "," +
                Thread.currentThread().getStackTrace()[3].getLineNumber() + "]" + "  " + log;
        if (log != null) {
            Log.i(tag, logAll);

        }
    }

    public static void error(String log) {
        String logAll = "[" + Thread.currentThread().getStackTrace()[3].getFileName() + "," +
                Thread.currentThread().getStackTrace()[3].getMethodName() + "," +
                Thread.currentThread().getStackTrace()[3].getLineNumber() + "]" + "  " + log;
        if (log != null) {
            Log.e(tag, logAll);

        }
    }

    public static void showLong(String msg){
        //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
        //  把4*1024的MAX字节打印长度改为2001字符数
        int max_str_length = 2001 - tag.length();
        //大于4000时
        while (msg.length() > max_str_length) {
            Log.i(tag, msg.substring(0, max_str_length));
            msg = msg.substring(max_str_length);
        }
        //剩余部分
        Log.i(tag, msg);
    }

    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("MM-dd HH:mm:ss.SSS",
            Locale.getDefault());

    /**
     * 将日志写入文件。
     *
     * @param tag
     * @param message
     */
    public static synchronized void writeLog(File logFile, String tag, String message) {
        logFile.getParentFile().mkdirs();
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String time = timeFormat.format(Calendar.getInstance().getTime());
        synchronized (logFile) {
            FileWriter fileWriter = null;
            BufferedWriter bufdWriter = null;
            PrintWriter printWriter = null;
            try {
                fileWriter = new FileWriter(logFile, true);
                bufdWriter = new BufferedWriter(fileWriter);
                printWriter = new PrintWriter(fileWriter);
                bufdWriter.append(time).append(" ").append("E").append('/').append(tag).append(" ")
                        .append(message).append('\n');
                bufdWriter.flush();
                printWriter.flush();
                fileWriter.flush();
            } catch (IOException e) {
                closeQuietly(fileWriter);
                closeQuietly(bufdWriter);
                closeQuietly(printWriter);
            }
        }
    }

    public static String readLog(File logFile) {
        String readOutStr = null;
        try {
            BufferedReader bufReader = new BufferedReader(new FileReader(logFile));
            String line = "";
            while(( line = bufReader.readLine()) != null) {
                readOutStr += line;
            }
            bufReader.close();
        } catch (IOException e) {
            readOutStr = null;
        }
        return readOutStr;
    }

    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ioe) {
                // ignore
            }
        }
    }
}
